www.gusucode.com > 串口测试程序,用于调试rs485接口 串口通信的程序 > 串口测试程序,用于调试rs485接口 串口通信的程序/commtest/MyCom.cpp
// MyCom.cpp: implementation of the CMyCom class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "stdafx.h" #include "CommTest.h" #include "MyCom.h" #include "CommTestDlg.h" #define LINELBYTES 25 #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CMyCom::CMyCom() { pCommTestDlg = FALSE; } CMyCom::~CMyCom() { } CMyCom::CMyCom(CCommTestDlg *pDlg) { pCommTestDlg = pDlg; } BOOL CMyCom::OnReceive() { BYTE buff[1024]; time_ttc1 = ::GetTickCount() - time_ttc; GetSystemTime( &m_time ); DWORD Len = Read( buff, sizeof(buff)); if( Len > 0) DisplayCommunicateCode(buff, Len, TRUE); return Len; } BOOL CMyCom::DisplayCommunicateCode(BYTE *buff, DWORD Len, BOOL Flags) { DWORD i = 0; CString str; char temp[LINELBYTES] = {0}; if(pCommTestDlg == NULL || Len == 0) return FALSE; if(!pCommTestDlg ->m_hex) { str =""; for(i = 0; i < Len; i++) { sprintf(temp,"%02x ",buff[i]); str = str + temp; if((i + 1) % LINELBYTES == 0) str += "\r\n"; } } else { str =""; for(i = 0; i < Len / LINELBYTES; i++) { strncpy(temp,(char *)&buff[i * LINELBYTES],LINELBYTES); str += temp; str += "\r\n"; } memset(temp,0,LINELBYTES); strncpy(temp,(char *)&buff[i * LINELBYTES],Len % LINELBYTES); str += temp; } pCommTestDlg->m_display += "\r\n"; if(Flags) { char tmp[20] = {0}; sprintf( tmp,"\n time = %d mis = %d \n", time_ttc1, m_time.wMilliseconds); pCommTestDlg->m_display += tmp; if(m_send) { m_send = false; pCommTestDlg->m_display += "接收:"; } } else pCommTestDlg->m_display += "发送:"; pCommTestDlg->m_display += "\r\n"; str.MakeUpper(); pCommTestDlg->m_display += str; pCommTestDlg->SendMessage(WM_USER + 100,0,0); return TRUE; } DWORD CMyCom::Write(BYTE *buff, DWORD bufflen) { DWORD Len = CAsyncCom::Write(buff, bufflen,FALSE); DisplayCommunicateCode(buff, bufflen,FALSE); time_ttc = ::GetTickCount(); m_send = true; return Len; } BOOL CMyCom::SetCom(COMM_STRUCT &t) { StopBits = t.stbit + 1; Parity = t.parity ; //##ModelId=3BDCCE3D03B4 ByteSize = t.dabit + 6; //##ModelId=3BDCCE3D03BD BaudRate = 1200; sComName.Format("\\\\.\\COM%d",t.comno + 1); // sComName.Format("\\\\.\\LPT1"); return TRUE; } //"LD_LIBRARY_PATH", "/lib:/usr/lib:/home/et1000" //export LD_LIBRARY_PATH="/lib:/usr/lib:/home/et1000" //insmod /data0/serial.o //fopen